package cn.iocoder.yudao.module.iot.controller.admin.room;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
//import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.iot.controller.admin.room.vo.IotRoomPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.room.vo.IotRoomRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.room.vo.IotRoomSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.room.IotRoomDO;
import cn.iocoder.yudao.module.iot.service.room.IotRoomService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
//import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - IoT 房间")
@RestController
@RequestMapping("/iot/room")
@Validated
public class IotRoomController {

    @Resource
    private IotRoomService roomService;

    @PostMapping("/create")
    @Operation(summary = "创建房间")
    @PreAuthorize("@ss.hasPermission('iot:room:create')")
    public CommonResult<Long> createRoom(@Valid @RequestBody IotRoomSaveReqVO createReqVO) {
        return success(roomService.createRoom(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新房间")
    @PreAuthorize("@ss.hasPermission('iot:room:update')")
    public CommonResult<Boolean> updateRoom(@Valid @RequestBody IotRoomSaveReqVO updateReqVO) {
        roomService.updateRoom(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除房间")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:room:delete')")
    public CommonResult<Boolean> deleteRoom(@RequestParam("id") Long id) {
        roomService.deleteRoom(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得房间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:room:query')")
    public CommonResult<IotRoomRespVO> getRoom(@RequestParam("id") Long id) {
        IotRoomDO room = roomService.getRoom(id);
        return success(BeanUtils.toBean(room, IotRoomRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得房间分页")
//    @PreAuthorize("@ss.hasPermission('iot:room:query')")
    public CommonResult<PageResult<IotRoomRespVO>> getRoomPage(@Valid IotRoomPageReqVO pageReqVO) {
        PageResult<IotRoomDO> pageResult = roomService.getRoomPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotRoomRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出房间 Excel")
    @PreAuthorize("@ss.hasPermission('iot:room:export')")
//    @OperateLog(type = EXPORT)
    public void exportRoomExcel(@Valid IotRoomPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotRoomDO> list = roomService.getRoomPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "房间.xls", "数据", IotRoomRespVO.class,
                BeanUtils.toBean(list, IotRoomRespVO.class));
    }

    @GetMapping("/list-by-user-id")
    @Operation(summary = "获得用户的房间列表")
    @Parameter(name = "userId", description = "用户编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:room:query')")
    public CommonResult<List<IotRoomRespVO>> getRoomListByUserId(@RequestParam("userId") Long userId) {
        List<IotRoomDO> list = roomService.getRoomListByUserId(userId);
        return success(BeanUtils.toBean(list, IotRoomRespVO.class));
    }

    @GetMapping("/list-by-parent-id")
    @Operation(summary = "获得子房间列表")
    @Parameter(name = "parentId", description = "父级房间编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:room:query')")
    public CommonResult<List<IotRoomRespVO>> getRoomListByParentId(@RequestParam("parentId") Long parentId) {
        List<IotRoomDO> list = roomService.getRoomListByParentId(parentId);
        return success(BeanUtils.toBean(list, IotRoomRespVO.class));
    }

    @GetMapping("/list-by-user-and-parent")
    @Operation(summary = "获得用户的子房间列表")
    @Parameter(name = "userId", description = "用户编号", required = true, example = "1024")
    @Parameter(name = "parentId", description = "父级房间编号", example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:room:query')")
    public CommonResult<List<IotRoomRespVO>> getRoomListByUserIdAndParentId(
            @RequestParam("userId") Long userId,
            @RequestParam(value = "parentId", required = false) Long parentId) {
        List<IotRoomDO> list = roomService.getRoomListByUserIdAndParentId(userId, parentId);
        return success(BeanUtils.toBean(list, IotRoomRespVO.class));
    }

}
